﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MagicArenaFramework.GameObject.Components;
using MagicArenaXNA.Games;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using MagicArenaXNA.Assets;
using MagicArenaXNA.GameObject.Spells;

namespace MagicArenaXNA.GameObject
{
    class UiBar : GoComponent
    {
        public delegate float GetValueDel();

        // Dels for fetching health and mana
        public GetValueDel GetMyHealth { get; set; }
        public GetValueDel GetTargetHealth { get; set; }
        public GetValueDel GetMyMana { get; set; }

        // Dels for seeing how long until action can be taken
        GetValueDel[] GetTimeAction { get;set; }

        SpriteBatch SpriteBatch;
        SpellBase[] Spells = new SpellBase[4];

        public UiBar(BepuGame game)
        {
            GetTimeAction = new GetValueDel[4];

            SpriteBatch = game.UIDrawer;
            SetGuiRect(game.Window);

            //Set GetValueDels
            GetMyHealth = ReturnDefault;
            GetMyMana = ReturnDefault;
            GetTargetHealth = ReturnDefault;

            //Set delegate
            this.GuiDel = MyGui;
        }

        float ReturnDefault(){
            return 0.8f;
        }

        void SetGuiRect(GameWindow window)
        {
            GetTimeAction = new GetValueDel[4];
            Rectangle screen = window.ClientBounds;
            int padVert = screen.Height/20;
            int width = (int)(GuiBarWidthActual*scale);
            int height = (int)(GuiBarHeightActual*scale);
            GuiBarRect = new Rectangle((screen.Width - width) / 2, screen.Height - padVert - height, width, height);
            width = (int)(GuiBarHealthWidthActual * scale);
            height = (int)(GuiBarHealthHeightActual * scale);
            GuiBarMyHealthRect = new Rectangle(GuiBarRect.Left + (int)(16 * scale), GuiBarRect.Top + (int)(90 * scale), width, height);
            GuiBarTarHealthRect = new Rectangle(GuiBarRect.Left + (int)(1262 * scale), GuiBarRect.Top + (int)(90 * scale), width, height);
            width = (int)(GuiBarManaWidthActual * scale);
            height = (int)(GuiBarManaHeightActual * scale);
            GuiBarMyManaRect = new Rectangle(GuiBarRect.Left + (int)(95 * scale), GuiBarRect.Top + (int)(16 * scale), width, height);
            width = (int)(SpellWidthActual * scale);
            height = (int)(SpellHeightActual * scale);
            GuiBarSpellRect = new Rectangle(GuiBarRect.Left + (int)(116 * scale), GuiBarRect.Top + (int)(417 * scale), width, height);
            GuiBarSpellOffset = (int)(SpellOffsetActual * scale);
        }

        #region Gui locaiton variables
        const float GuiBarWidthActual = 1340, GuiBarHeightActual = 705;
        const float GuiBarHealthWidthActual = 60, GuiBarHealthHeightActual = 600;
        const float GuiBarManaWidthActual = 1150, GuiBarManaHeightActual = 62;
        const int SpellWidthActual = 256, SpellHeightActual = 256, SpellOffsetActual=284;
        float scale = 0.25f;
        Rectangle GuiBarRect;
        Rectangle GuiBarMyHealthRect;
        Rectangle GuiBarTarHealthRect;
        Rectangle GuiBarMyManaRect;
        Rectangle GuiBarSpellRect;
        int GuiBarSpellOffset;
        #endregion

        void MyGui(float dt)
        {
            AddGuiBar(dt);
        }

        #region GuiBar
        void AddGuiBar(float dt)
        {
            TestAssetHolder asset = TestAssetHolder.Instance;

            //Add health and mana meters
            Texture2D item = asset.UiBarHealth;
            float val = GetMyHealth();
            val = val < 0 ? 0 : val;
            val = val > 1 ? 1 : val;
            int offset = (int)(GuiBarHealthHeightActual * val);
            SpriteBatch.Draw(item, new Vector2(GuiBarMyHealthRect.X, GuiBarMyHealthRect.Y + GuiBarMyHealthRect.Height * (1-val)), new Rectangle(0, (int)GuiBarHealthHeightActual - offset, (int)(GuiBarHealthWidthActual), offset), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

            item = asset.UiBarHealth;
            val = GetTargetHealth();
            val = val < 0 ? 0 : val;
            val = val > 1 ? 1 : val;
            offset = (int)(GuiBarHealthHeightActual * val);
            SpriteBatch.Draw(item, new Vector2(GuiBarTarHealthRect.X, GuiBarTarHealthRect.Y + GuiBarTarHealthRect.Height * (1 - val)), new Rectangle(0, (int)GuiBarHealthHeightActual - offset, (int)(GuiBarHealthWidthActual), offset), Color.White, 0, Vector2.Zero, scale, SpriteEffects.FlipHorizontally, 0);

            item = asset.UiBarMana;
            val = GetMyMana();
            val = val < 0 ? 0 : val;
            val = val > 1 ? 1 : val;
            offset = (int)(GuiBarManaWidthActual*val);
            SpriteBatch.Draw(item, new Vector2(GuiBarMyManaRect.X, GuiBarMyManaRect.Y), new Rectangle((int)(GuiBarManaWidthActual-offset), 0, offset, (int)GuiBarManaHeightActual), Color.White, 0, Vector2.Zero, scale, SpriteEffects.FlipHorizontally, 0);


            // Add background
            item = asset.UiBarBackground;
            SpriteBatch.Draw(item, GuiBarRect, Color.White);

            Texture2D spellFrame = asset.UiBarSpellFrame;
            Texture2D spellOverlay = asset.UiBarSpellOverlay;
            SpellBase spell;
            // Add spells
            //TODO make into for loop, this is REALLY sloppy code
            item = asset.FireSpell;
            Rectangle spellRect = new Rectangle(GuiBarSpellRect.X, GuiBarSpellRect.Y, GuiBarSpellRect.Width, GuiBarSpellRect.Height);
            SpriteBatch.Draw(item, spellRect, Color.White);
            float coolPerc;
            spell = Spells[0];
            coolPerc = spell == null ? 0.25f : (spell.CurrentCooldownPercent);
            offset = (int)(SpellHeightActual*(coolPerc));
            SpriteBatch.Draw(spellOverlay, new Vector2(spellRect.X, spellRect.Y + (SpellHeightActual - offset) * scale), new Rectangle(0, (int)(SpellHeightActual - offset), (int)SpellWidthActual, offset), Color.White, 0, Vector2.Zero, scale, SpriteEffects.FlipVertically, 0);
            SpriteBatch.Draw(spellFrame, spellRect, Color.White);
            

            item = asset.HealSpell;
            spellRect.X+=GuiBarSpellOffset;
            SpriteBatch.Draw(item, spellRect, Color.White);
            coolPerc = Spells[1] == null ? 0.25f : Spells[1].CurrentCooldownPercent;
            offset = (int)(SpellHeightActual * (coolPerc));
            SpriteBatch.Draw(spellOverlay, new Vector2(spellRect.X, spellRect.Y + (SpellHeightActual - offset) * scale), new Rectangle(0, (int)(SpellHeightActual - offset), (int)SpellWidthActual, offset), Color.White, 0, Vector2.Zero, scale, SpriteEffects.FlipVertically, 0);
            SpriteBatch.Draw(spellFrame, spellRect, Color.White);

            item = asset.ShieldSpell;
            spellRect.X += GuiBarSpellOffset;
            SpriteBatch.Draw(item, spellRect, Color.White);
            coolPerc = Spells[2] == null ? 0.25f : Spells[2].CurrentCooldownPercent;
            offset = (int)(SpellHeightActual * (coolPerc));
            SpriteBatch.Draw(spellOverlay, new Vector2(spellRect.X, spellRect.Y + (SpellHeightActual - offset) * scale), new Rectangle(0, (int)(SpellHeightActual - offset), (int)SpellWidthActual, offset), Color.White, 0, Vector2.Zero, scale, SpriteEffects.FlipVertically, 0);
            SpriteBatch.Draw(spellFrame, spellRect, Color.White);

            item = asset.SummonSpell;
            spellRect.X += GuiBarSpellOffset;
            SpriteBatch.Draw(item, spellRect, Color.White);
            coolPerc = (Spells[3] == null) ? 0.25f : Spells[3].CurrentCooldownPercent;
            offset = (int)(SpellHeightActual * (coolPerc));
            SpriteBatch.Draw(spellOverlay, new Vector2(spellRect.X, spellRect.Y + (SpellHeightActual - offset)*scale), new Rectangle(0, (int)(SpellHeightActual - offset), (int)SpellWidthActual, offset), Color.White, 0, Vector2.Zero, scale, SpriteEffects.FlipVertically, 0);
            SpriteBatch.Draw(spellFrame, spellRect, Color.White);
        }
        
        #endregion

        #region RegisterSpells
        public void SetSpells(SpellBase[] spells)
        {
            int i;
            for (i = 0; i < spells.Length && i < 4; i++)
            {
                Spells[i] = spells[i];
            }
        }
        #endregion
    }
}
